#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e9+7;
vector<int> v;
int n,s,a1,b;
bool check(int mid)
{
	for(int i=mid;i<=n;i++)
	{
		if((v[i]-v[i-mid])>=s)return true;
	}
	return false;
}
signed main()
{
	
	cin>>n>>s;
	cin>>a1>>b;
	v.push_back(0);
	v.push_back(a1);
	int sum=a1;
	int last=a1;
	for(int i=2;i<=n;i++)
	{
		int now=(last*b)%N;
		v.push_back(v[i-1]+now);
		last=now;
		sum+=now;
	}
	//cout<<sum<<endl;
	if(sum<s){
		cout<<-1;
		return 0;
	}
	//for(auto p:v)cout<<p<<" ";
	//cout<<endl;
	int l=1,r=n;
	while(l<=r)
	{
		int mid=l+r>>1;
		if(!check(mid))
		{
			l=mid+1;
		}else
		{
			r=mid-1;
		}
	}
	cout<<l;
	return 0;;
}
